//
//  MWAboutController.m
//  XiRan-iOS
//
//  Created by Richfit on 2019/10/30.
//  Copyright © 2019 Wood. All rights reserved.
//

#import "MWAboutController.h"
#import <WebKit/WebKit.h>

@interface MWAboutController () <WKUIDelegate, WKNavigationDelegate>

@property(nonatomic, strong) UIProgressView *progressView;
@property(nonatomic, strong) WKWebView *webView;

@end

@implementation MWAboutController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = [[MWConfigManager manager] objectForKey:@"about_us" defaultValue:@"关于我们"];
    
    [self setupPageSubviews];
    [self loadConfig];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
    if ([keyPath isEqual:@"estimatedProgress"] && object == self.webView) {
        [self.progressView setAlpha:1.0f];
        [self.progressView setProgress:self.webView.estimatedProgress animated:YES];
        if (self.webView.estimatedProgress  >= 1.0f) {
            [UIView animateWithDuration:0.3 delay:0.3 options:UIViewAnimationOptionCurveEaseOut animations:^{
                [self.progressView setAlpha:0.0f];
            } completion:^(BOOL finished) {
                [self.progressView setProgress:0.0f animated:YES];
            }];
        }
    }else{
        [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
    }
}

- (void)dealloc {
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    [self.webView setNavigationDelegate:nil];
    [self.webView setUIDelegate:nil];
}

- (void)loadURLWithDic:(NSDictionary *)dic {
    NSString *domian = [dic objectForKey:@"domain"];
//    NSString *url = [dic objectForKey:@"url"];
    self.webView.hidden = NO;
    self.errorTitleLabel.hidden = self.errorImageView.hidden = YES;
    NSString *urlStr;
    if ([[MWConfigManager manager].language isEqualToString:@"0"]) {
        NSString *z_about = [dic objectForKey:@"z_about"];
        urlStr = [NSString stringWithFormat:@"%@%@", domian, z_about];
    } else if ([[MWConfigManager manager].language isEqualToString:@"2"]) {
        NSString *j_about = [dic objectForKey:@"j_about"];
        urlStr = [NSString stringWithFormat:@"%@%@", domian, j_about];
    } else {
        NSString *e_about = [dic objectForKey:@"e_about"];
        urlStr = [NSString stringWithFormat:@"%@%@", domian, e_about];
    }
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
}

- (void)loadConfig {
    NSString *token = [MWUserManager manager].user.token;
    NSMutableDictionary *paraDic = [[NSMutableDictionary alloc] init];
    [paraDic setObject:token forKey:@"token"];
    [paraDic setObject:[MWConfigManager manager].productType forKey:@"is_material"];
    
    MWWeakSelf
    [[MWNetworkManager manager] POST:kConfigUrl parameters:paraDic progress:^(NSProgress *downloadProgress) {
        
    } success:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSInteger code = [[dic objectForKey:@"code"] integerValue];
        NSString *msg = [dic objectForKey:@"msg"];
        if (code == 1) {
            NSDictionary *dataDic = [dic objectForKey:@"data"];
            [weakSelf loadURLWithDic:dataDic];
            
        } else {
            
        }
    } failure:^(NSError *error) {
        if ([MWConfigManager manager].isNetError) {
            self.errorTitleLabel.text = [[MWConfigManager manager] objectForKey:@"click_retry" defaultValue:@"点击重试"];
            self.errorTitleLabel.hidden = self.errorImageView.hidden = NO;
            self.webView.hidden = YES;
        }
    }];
}

- (void)refresh {
    [super refresh];
    [self loadConfig];
}

- (void)setupPageSubviews {
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    WKPreferences *preference = [[WKPreferences alloc]init];
    preference.javaScriptEnabled = YES;
    preference.javaScriptCanOpenWindowsAutomatically = YES;
    config.preferences = preference;
    
    _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
    _webView.UIDelegate = self;
    _webView.navigationDelegate = self;
    _webView.allowsBackForwardNavigationGestures = YES;
    [_webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew context:nil];
    [self.view addSubview:self.webView];
    
    _progressView = [[UIProgressView alloc] init];
    _progressView.progressTintColor = [UIColor greenColor];
    [self.view addSubview:_progressView];
    
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
    [_progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self.view);
        make.height.equalTo(@2);
    }];
}


@end
